package com.xiaomi.controller;

import com.xiaomi.pojo.Cart;
import com.xiaomi.pojo.Goods;
import com.xiaomi.pojo.User;
import com.xiaomi.service.CartService;
import com.xiaomi.service.GoodsService;
import com.xiaomi.service.Impl.GoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName CartService
 * @Author xiao宋
 * @Date 15:11  2022/9/14
 * @Version 1.0
 * @Description TODO
 */
@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartService cartService;

    /**
     * 清除购物车（Ajax）
     */
    @ResponseBody
    @RequestMapping("/clearCartAjax")
    public void clearCartAjax(User user) {
        System.out.println("clearCartAjax");
        cartService.clearCart(user.getId());
    }

    /**
     * 添加购物车（Ajax部分）
     */
    @ResponseBody
    @RequestMapping("/addCartAjax")
    public void addCartAjax(HttpSession session, int goodsId, int number) {
        System.out.println("addCart");
        System.out.println("增删购物车处接受到的number"+number);
        User user =(User) session.getAttribute("user");
        //获取增删数据
        if (number == 0) {
            cartService.deleteCart(goodsId, user.getId());
        } else {
            GoodsService goodsService = new GoodsServiceImpl();
            Goods goods = goodsService.findById(goodsId);
            Cart cart = cartService.findCartByUidAndGoodsId(user.getId(), goodsId);
            if (cart == null) {
                cart = new Cart();
                cart.setId(user.getId());
                cart.setNum(1);
                cart.setPid(goodsId);
                cart.setMoney(goods.getPrice());
                cartService.insert(cart);
            } else {
                cart.setNum(cart.getNum() + number);
                cart.setMoney(cart.getNum() * goods.getPrice());
                cartService.update(cart);
            }
        }
    }

    /**
     * 添加购物车
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/addCart")
    public String addCart(HttpSession session, Integer goodsId, Integer number, ModelAndView mv) {
        System.out.println("addCart接收到的number： " + number);
        System.out.println(goodsId);
//        从session里拿到 user 信息
        User user =(User) session.getAttribute("user");
        Cart cart = cartService.findCartByUidAndGoodsId(user.getId(), goodsId);
        GoodsService goodsService = new GoodsServiceImpl();
        Goods goods = goodsService.findById(goodsId);
        double price = goods.getPrice();
        if (cart == null) {
            //购物车是空的就新建一个商品信息
            cart = new Cart();

            cart.setMoney(number * cart.getMoney());
            cart.setPid(goodsId);
            cart.setId(user.getId());
            cart.setNum(number);
            cart.setMoney(number * price);
            cartService.insert(cart);
        }//如果购物车不是空的就添加一个
        else {
            cart.setNum(cart.getNum() + number);
            cart.setMoney(cart.getNum() * (int) price);
            cartService.update(cart);
        }

        return "redirect:/cartSuccess.jsp";
    }

    /**
     * 获取购物车信息
     * @return
     */
    @RequestMapping("/getCart")
    public ModelAndView getCart(HttpSession session) {
        User user = (User) session.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        System.out.println("addCart处接收到的用户id；" + user.getId());
        int id = user.getId();
        List<Cart> carts = cartService.getCarts(id);
//        return "redirect:/cart.jsp";
        mv.addObject("carts", carts);
        System.out.println(carts);
//        return carts;
        mv.setViewName("forward:/cart.jsp");
        return mv;
    }
}
